package com.samphanie.auiu.biz.account

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.samphanie.auiu.R
import com.samphanie.auiu.common.ui.component.BaseAppBindingActivity
import com.samphanie.auiu.databinding.ActivityLoginBinding
import com.samphanie.auiu.model.TokenInfo
import com.samphanie.auiu.http.ApiFactory
import com.samphanie.auiu.http.api.AccountApi
import com.samphanie.auiu.library.log.AuiuLog
import com.samphanie.auiu.library.restful.AuiuCallback
import com.samphanie.auiu.library.restful.AuiuResponse
import com.samphanie.auiu.route.AuiuRoute

/**
 * @author ZSY
 */
@Route(path = "/account/login" )
class LoginActivity : BaseAppBindingActivity<ActivityLoginBinding>() {

    private val REQUEST_CODE_REGISTRATION: Int = 1000

    override fun getLayoutId(): Int {
        return R.layout.activity_login
    }

    override fun initialize(savedInstanceState: Bundle?) {}

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // action_back
        binding.actionBack.setOnClickListener {
            backPress(this)
        }

        binding.actionRegister.setOnClickListener {
            goRegistration()
        }

        binding.actionLogin.setOnClickListener {
            goLogin()
        }

    }

    private fun goLogin() {
        val username = binding.inputItemUsername.getEditText().text
        val password = binding.inputItemPassword.getEditText().text

        AuiuLog.e("LoginActivity", "用户名：$username  密码：$password")

        if (username.isEmpty() || password.isEmpty()) {
            return
        }

        // TODO viewModel + repository + livaData
        ApiFactory.create(AccountApi::class.java).login(
            "android-client","123456",
            username.toString(), password.toString()
        ).enqueue(object : AuiuCallback<TokenInfo> {
                override fun onSuccess(response: AuiuResponse<TokenInfo>) {
                    if (response.successful() && response.data != null) {
                        showToast(getString(R.string.login_success))
                        val data = response.data

                        // userManager
                        // data!!.token?.let { SPUtil.putString("boarding-pass", it) }
                        AccountManager.loginSuccess(data!!)
                        setResult(Activity.RESULT_OK)
                        finish()
                    } else {
                        showToast(getString(R.string.login_failed) + response.message)
                    }
                }

                override fun onFailed(throwable: Throwable) {
                    showToast(getString(R.string.login_failed) + throwable.message)
                }

            })
    }

    private fun goRegistration() {
        AuiuRoute.startActivity(
            this,
            destination = AuiuRoute.Destination.GOODS_LIST,
            requestCode = REQUEST_CODE_REGISTRATION
        )
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK && data != null && REQUEST_CODE_REGISTRATION == requestCode) {
            val username = data.getStringExtra("username")
            if (!username.isNullOrBlank()) {
                binding.inputItemUsername.getEditText().setText(username)
            }
        }
    }

}